<?php

require_once('Zend/Db/Table/Row/Abstract.php');

class Core_ISO_639
	extends Zend_Db_Table_Row_Abstract
{
	/**
	 * Private static reference to the ISO Code table.
	 * 
     * @return Zend_Db_Table
	 */
	private static $table;
	
    /**
     * Name of the class of the Zend_Db_Table_Abstract object.
     *
     * @var string
     */
    protected $_tableClass = 'Zend_Db_Table';

    /**
     * Select access method, for slightly more complex queries.
     *
     * @return Zend_Db_Table_Select
     */
    private static function getTableInstance()
    {
    	if ( empty(Core_ISO_639::$table ) )
    	{
    		Core_ISO_639::$table = new Zend_Db_Table('iso_639');
    		Core_ISO_639::$table->setRowClass('Core_ISO_639');
    	}
    	
    	return Core_ISO_639::$table;
    }
    
    /**
     * Factory Method. Constructs a DB-connected data instance for the
     * passed code or unique ID.
     *
     * @return Core_ISO_639
     */
    public static function factory( $id_or_code )
    {
    	$table = Core_ISO_639::getTableInstance();

    	if ( is_int($id_or_code) )
    	{
    		$row = $table->fetchRow( $table->select()->where('id=?',$id_or_code));
    	}
    	else
    	{
    		$select = $table->select();
    		$select->where('part_1=?',$id_or_code);
    		$select->where('part_2b=?',$id_or_code);
    		$select->where('part_2c=?',$id_or_code);
    		$select->where('part_3=?',$id_or_code);
    		$row = $table->fetchRow( $select );
    	}
    	
    	return $row;
    }
    
    /**
     * Overriding the save method: You cannot save into a standards table.
     */
    public function save()
    {
    	throw new InvalidArgumentException('ISO Code Tables cannot be altered', 0);
    }
}